package com.ityuan.springcloud.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ityuan.springcloud.client.user.entity.User;
import com.ityuan.springcloud.service.OrderService;
import com.ityuan.springcloud.service.UserFreignService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private UserFreignService userFreignService;

	/**
	 * @see https://blog.csdn.net/wk19920726/article/details/107917146
	 */
	@HystrixCommand(fallbackMethod = "fallbackGetOrderUserById", commandProperties = {
			@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5") })
	public User getOrderUserById(Long userId) {
		try {
			// 模拟触发熔断场景
			Thread.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return this.userFreignService.getUserById(userId);
	}

	public User fallbackGetOrderUserById(Long userId) {
		log.error("========>断路器fallback");
		User user = new User();
		user.setName("hystrix熔断测试");
		user.setAge(-1);
		return user;
	}

	public User getOrderUser2ById(Long userId) {
		return this.userFreignService.getUserById(userId);
	}

}